package com.joseph.websocket.util;

import com.alibaba.fastjson.JSON;
import com.joseph.websocket.bean.Message;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import org.jctools.queues.MpscChunkedArrayQueue;

import java.nio.channels.SocketChannel;

public class MessageUtil {

    public static void send(ChannelHandlerContext ctx, Message message) {
        if (ctx.channel() != null && ctx.channel().isActive() && ctx.channel().isWritable()) {
            ChannelFuture cf = ctx.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(message)));
            cf.addListener(new GenericFutureListener<Future<? super Void>>() {
                @Override
                public void operationComplete(Future<? super Void> future) throws Exception {
                    if (future.isSuccess()) {

                    }
                }
            });
        }
    }
}
